const webpack = require('webpack');
const path = require('path');
const CleanPlugin = require('clean-webpack-plugin') //webpack插件，用于清除目录文件
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const {
  Utils
} = require('./utils');
const SrcPath = path.resolve(__dirname, 'src');
const distPath = path.resolve(__dirname, 'dist');
const entryMap = Utils.getEntryMap();
const htmlArray = Utils.getHtmlArray(entryMap);


module.exports = {
  mode: 'production',
  entry: entryMap,
  resolve: {
    alias: {
      component: path.resolve(SrcPath, 'component')
    },
    extensions: ['.js', '.jsx']
  },
  output: {
    path: distPath,
    filename: 'js/[name].[hash].min.js',
    publicPath: '/' // 可根据自己实际情况修改
  },
  module: {
    rules: [
        {
            test: /\.(js|jsx|ts|tsx)?$/,
            exclude: /(node_modules|bower_components)/,
            use: [
                'babel-loader',
                'eslint-loader'
            ]
        },
        {
            test: /\.css$/,
            use: [
                MiniCssExtractPlugin.loader,
                {
                    loader: 'css-loader',
                    options: {
                        minimize: true
                    }
                },
                'postcss-loader'
            ],
            include: SrcPath
        },
        {
            test: /\.scss$/,
            use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader',{
                loader: 'sass-resources-loader',
                options: {
                    resources: SrcPath + '/common/scss_fun.scss'
                }
            }],
            include: SrcPath
        },
        {
            test: /\.less$/,
            use: [MiniCssExtractPlugin.loader,
                {
                    loader: 'css-loader',
                    options: {
                        importLoaders: 1
                    }
                },
                {
                    loader: 'postcss-loader'
                },
                {
                    loader: 'less-loader' // 如果less文件中使用了@import 引入了别的less文件，这里可以不用设置importLoaders
                }
            ]
        },
        {
            test: /\.(png|jpg|jpeg|gif|svg)$/,
            use: [{
                loader: 'url-loader',
                options: {
                    limit: 20000,
                    name: './images/[name].[hash].[ext]'
                }
            }],
            exclude: /(node_modules|bower_components)/,
        },
        {
            test: /\.(woff2|eot|ttf|otf)$/,
            use: [{
                loader: 'url-loader',
                options: {
                    limit: 100000,
                    name: 'fonts/[name]-[hash:7].[ext]'
                }
            }],
            exclude: /(node_modules|bower_components)/,
        },
        {
            test: /\.json$/,
            loader: 'json-loader'
        }
    ]
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        common: {
          test: /[\\/]node_modules[\\/]/,
          chunks: 'all',
          name: 'common'
        }

      }
    }
  },
  plugins: [
    new CleanPlugin(['dist'], { // 清除dist
      root: path.join(__dirname, './')
    }),
    new CopyWebpackPlugin([
          { from: 'src/json', to: path.resolve(distPath, 'json'), force: true },
          { from: 'src/static', to: path.resolve(distPath, 'static'), force: true }
      ]),
    new MiniCssExtractPlugin({
      filename: "css/[name].[hash].css",
    })
  ].concat(htmlArray)
}
